home *** CD-ROM | disk | FTP | other *** search
- Path: news.sprintlink.net!datalytics!usenet
- From: Rob Stewart <stew@datalytics.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Help! What's wrong with this?
- Date: Thu, 18 Apr 1996 11:32:54 -0400
- Organization: Datalytics, Inc
- Message-ID: <317660A6.56C7@datalytics.com>
- References: <4l22v3$1beo@useneta1.news.prodigy.com>
- NNTP-Posting-Host: 204.62.224.71
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Brian Munroe wrote:
- >
- > I'm trying to learn C++ and I've run into a strange problem in the
- > following code.
- >
-
- First, the code you submitted is C, not C++. Just because
- you're using a C++ compiler on it does not make it C++. Let's
- call it what it is.
-
- > int const ADD_LEN = 40;
-
- Try "const int ADD_LEN = 40" instead.
-
- > int main()
- > {
- > struct cust_list {
- > char name[ADD_LEN];
- > char add1[ADD_LEN];
- > char add2[ADD_LEN];
- > char add3[ADD_LEN];
- > } customer;
- > int flag;
- >
- > void get_customer_info(char temp[ADD_LEN], int flag);
-
- This declaration should really come before main.
-
- >
- > for (flag=0; flag<4; flag++)
- > { switch (flag)
- > { case 0: get_customer_info(customer.name, flag); break;
- > case 1: get_customer_info(customer.add1, flag); break;
- > case 2: get_customer_info(customer.add2, flag); break;
- > case 3: get_customer_info(customer.add3, flag); break;
- > default: exit(1);
- > }
- > }
-
- This is the oddest thing I've ever seen. Don't play games like
- this, just write the following:
-
- get_customer_info(customer.name, 0);
- get_customer_info(customer.add1, 1);
- get_customer_info(customer.add2, 2);
- get_customer_info(customer.add3, 3);
-
- Having said that, it would be better for you to supply the
- prompt string to get_customer_info rather than using a flag to
- do it. This allows the caller to more appopriately associate
- the text of the prompt with the buffer being filled.
-
- > return 0;
- > }
- >
- > void get_customer_info(char temp[ADD_LEN], int flag)
-
- FYI: Declaring temp as char[ADD_LEN] doesn't guarrantee that you
- get an array of that size.
-
- > { int answer;
- > int len;
- > clrscr();
- > switch (flag)
- > { case 0:
- > cout << "Enter the bidder's name in one of the following forms.\n";
- > cout << " Person: LAST NAME, FIRST NAME\n";
- > cout << " Business: COMPANY NAME\n\n";
- > break;
- > case 1:
- > cout << "\nEnter first line of address\n";
- > break;
- > case 2:
- > cout << "\nEnter second line of address\n";
- > break;
- > case 3:
- > cout << "\nEnter third line of address\n";
- > break;
- > default:
- > exit(1);
- > }
- > cin >> temp;
-
- Try this instead. It will eat whitespace before filling temp.
-
- cin >> ws >> temp;
-
- > len = strlen(temp);
- > if (len < ADD_LEN)
- > { strncat(temp, " ",ADD_LEN-
- > len);}
- > temp[ADD_LEN-1] = '\0';
-
- Rather than have get_customer_info be dependent upon a global
- value, why not pass the size of temp to it. This reduces
- dependence on the global to main, which is a better habit to
- develop (later maintenance of code is easier with restricted
- dependence on globals).
-
- > return;
- > }
- >
- > When I run this all 4 prompts are outputted (Is that a word?), but only
- > input for the name and second line are accepted. The input line doesn't
- > accept input for the first and third lines. I'm guessing that there is a
- > return character caught in the input buffer after I input the name and
- > second address lines and it's read in for the first and third lines. If
- > so, how do I get it out of the buffer and what the heck is it doing there
- > in the first place !?
- >
- > Thanks for your help.
-
- --
- Robert Stewart | My opinions are usually my own.
- Datalytics, Inc. | stew@datalytics.com
-